package com.tools.scanner.ad.impl

import android.app.Activity
import android.view.View
import com.google.android.gms.ads.AdError
import com.google.android.gms.ads.FullScreenContentCallback
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.OnUserEarnedRewardListener
import com.google.android.gms.ads.admanager.AdManagerAdRequest
import com.google.android.gms.ads.rewarded.RewardItem
import com.google.android.gms.ads.rewarded.RewardedAd
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback
import com.tools.scanner.QrApp

class RewardedAdImpl(mAdId: String, mLocation: String) : BaseAdImpl(mAdId, mLocation) {

    private var mRewardedAd: RewardedAd? = null

    override fun beginLoad(adLocation: String, adId: String) {
        val adRequest: AdManagerAdRequest = AdManagerAdRequest.Builder().build()
        RewardedAd.load(
            QrApp.app, adId, adRequest,
            object : RewardedAdLoadCallback() {
                override fun onAdLoaded(rewardedAd: RewardedAd) {
                    super.onAdLoaded(rewardedAd)
                    onLoadFinish(true, rewardedAd)
                }

                override fun onAdFailedToLoad(loadAdError: LoadAdError) {
                    super.onAdFailedToLoad(loadAdError)
                    val errorCode = loadAdError.code
                    val errorMessage = loadAdError.message

                    onLoadFinish(false, null, errorCode, errorMessage)
                }

                private fun onLoadFinish(
                    isSuccess: Boolean,
                    rewardedAd: RewardedAd? = null,
                    errCode: Int = -1,
                    errMsg: String = ""
                ) {
                    mRewardedAd = rewardedAd
                    val adapterName = rewardedAd?.responseInfo?.mediationAdapterClassName ?: ""
                    notifyAdLoadEnd(errCode, errMsg, adapterName, isSuccess)
                }
            })
    }

    override fun beginShow(activity: Activity, adLocation: String, adId: String) {
        val currentAd = mRewardedAd ?: return

        currentAd.fullScreenContentCallback = object : FullScreenContentCallback() {
            override fun onAdShowedFullScreenContent() {
                notifyAdShow()
            }

            override fun onAdClicked() {
                notifyClick()
            }

            override fun onAdDismissedFullScreenContent() {
                notifyAdClose()
            }

            override fun onAdFailedToShowFullScreenContent(adError: AdError) {
                notifyShowFail(adError.code, adError.message)
            }
        }

        currentAd.setOnPaidEventListener {
            notifyAdPaid(it)
        }

        try {
            notifyAdPrepareShow()
            currentAd.show(activity, object : OnUserEarnedRewardListener {
                override fun onUserEarnedReward(p0: RewardItem) {
                    notifyAdRewarded()
                }
            })
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            mRewardedAd = null
        }
    }

    override fun isAdAvailable(): Boolean {
        return mRewardedAd != null
    }

    override fun getNativeView(): View? {
        return null
    }

}